package com.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.store.configure.Result;
import com.store.entity.*;
import com.store.entity.Notice;
import com.store.mapper.*;
import com.store.service.NoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.store.utils.IDGenerator;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2025-01-04
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Resource
    NoticeMapper noticeMapper;

    @Resource
    NoticeUserMapper noticeUserMapper;

    @Resource
    UserMapper userMapper;

    @Override
    public Result<Notice> updateNotice(Notice notice) {
        Result<Notice> result = new Result<>();
        try {
            if (StringUtils.isEmpty(notice.getNoticeId())) {
                //如果ID为空，代表添加
                notice.setNoticeId(IDGenerator.getUUID("NOTICE"));
                notice.setCreateTime(new Date());
                notice.setUpdateTime(new Date());
                noticeMapper.insert(notice);

                QueryWrapper<User> wrapper = new QueryWrapper<>();
                wrapper.eq("user_type", "2");
                if(notice.getUserCheckList() != null && notice.getUserCheckList().length > 0){
                    wrapper.in("user_id", notice.getUserCheckList());
                }
                List<User> users = userMapper.selectList(wrapper);

                for (User user : users) {
                    NoticeUser noticeUser = new NoticeUser();
                    noticeUser.setNoticeUserId(IDGenerator.getUUID("NOTICE"));
                    noticeUser.setUserId(user.getUserId());
                    noticeUser.setNoticeId(notice.getNoticeId());
                    noticeUser.setNoticeStatus("0");
                    noticeUser.setTitle(notice.getTitle());
                    noticeUser.setContent(notice.getContent());
                    noticeUser.setCreaterId(notice.getCreaterId());
                    noticeUser.setCreaterName(notice.getCreaterName());
                    noticeUser.setCreateTime(new Date());
                    noticeUser.setUpdateTime(new Date());
                    noticeUserMapper.insert(noticeUser);
                }

            } else {
                //如果ID不为空，代表修改
                notice.setUpdateTime(new Date());
                noticeMapper.updateById(notice);

                QueryWrapper<NoticeUser> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("notice_id", notice.getNoticeId());
                List<NoticeUser> noticeUsers = noticeUserMapper.selectList(queryWrapper);
                for (NoticeUser noticeUser : noticeUsers) {
                    noticeUser.setTitle(notice.getTitle());
                    noticeUser.setContent(notice.getContent());
                    noticeUser.setUpdateTime(new Date());
                }
            }
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<Notice> delNotice(Notice notice) {
        Result<Notice> result = new Result<>();
        try {
            if (StringUtils.isEmpty(notice.getNoticeId())) {
                result.setMessage("noticeId can't be empty");
                return result;
            }
            noticeMapper.deleteById(notice.getNoticeId());
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<Notice> batchDelNotice(Notice notice) {
        Result<Notice> result = new Result<>();
        try {
            if (!StringUtils.isEmpty(notice.getIds())) {
                String[] ids = notice.getIds().split(",");
                for (String id : ids) {
                    noticeMapper.deleteById(id);
                }
            }
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<Notice> queryNotice(Notice notice) {
        Result<Notice> result = new Result<>();
        try {
            QueryWrapper<Notice> wrapper = new QueryWrapper<>();
            //查询条件拼接，仅限单表查询可用
            List<Notice> notices = noticeMapper.selectList(wrapper);
            result.setReturnList(notices);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<Notice> queryNoticeByPage(Notice notice) {
        Result<Notice> result = new Result<>();

        try {
            if (notice.getCurrent() == null || notice.getSize() == null) {
                result.setMessage("current or size can't be null");
                return result;
            }

            QueryWrapper<Notice> wrapper = new QueryWrapper<>();
            if (!StringUtils.isEmpty(notice.getTitle())) {
                wrapper.lambda().like(Notice::getTitle, notice.getTitle());
            }

            IPage<Notice> notices = noticeMapper.selectPage(new Page<>(notice.getCurrent(), notice.getSize()), wrapper);
            result.setReturnPage(notices);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<Notice> queryNoticeById(Notice notice) {
        Result<Notice> result = new Result<>();
        try {
            Notice returnNotice = noticeMapper.selectById(notice.getNoticeId());
            result.setReturnObj(returnNotice);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

}
